package com.hq.fxh.No5_User.User.Activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Modle.FollowUserModle;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No5_User.User.Adapter.Ladapter_GZ_People;
import com.hq.fxh.R;
import com.hq.fxh.Utils.ClickUtils;
import com.hq.fxh.Utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.data;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/27
 * @Email xlwenhao1858@sina.com
 */


public class BlackListActivity extends BaseActivity implements View.OnClickListener {
    protected ImageView toolbarImageBack;
    protected LinearLayout toolbarLayoutBack;
    protected TextView toolBarTvTitle;
    protected TextView toolBarTvMore;
    protected LinearLayout toolbarLayoutMore;
    protected LinearLayout toolbarContent;
    protected PullToRefreshListView listFenSi;
    int page = 1;
    private Ladapter_GZ_People adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_fen_si;
    }

    @Override
    protected void initView() {
        toolbarImageBack = (ImageView) findViewById(R.id.toolbar_image_back);
        toolbarLayoutBack = (LinearLayout) findViewById(R.id.toolbar_Layout_back);
        toolbarLayoutBack.setOnClickListener(BlackListActivity.this);
        toolBarTvTitle = (TextView) findViewById(R.id.toolBar_tv_Title);
        toolBarTvMore = (TextView) findViewById(R.id.toolBar_tv_More);
        toolbarLayoutMore = (LinearLayout) findViewById(R.id.toolbar_Layout_More);
        toolbarContent = (LinearLayout) findViewById(R.id.toolbar_Content);
        listFenSi = (PullToRefreshListView) findViewById(R.id.list_FenSi);

    }

    @Override
    protected void initData() {
        toolBarTvTitle.setText("黑名单列表");
        toolBarTvMore.setVisibility(View.INVISIBLE);


        listFenSi.setMode(PullToRefreshBase.Mode.BOTH);
        listFenSi.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                refreshData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = page + 1;
                refreshData();
            }
        });
        toolBarTvTitle.setText("我的粉丝");
        toolBarTvMore.setVisibility(View.INVISIBLE);
        adapter = new Ladapter_GZ_People();
        adapter.setCallBack(new Ladapter_GZ_People.GZCallBack() {
            @Override
            public void call(TextView textView, int position, FollowUserModle.DataBean user) {
                if (user != null) {
                    int state = user.getState();
                    if (state == 1) {
                        user.setState(0);
                    } else {
                        user.setState(1);
                    }
                }
            }
        });

        listFenSi.setAdapter(adapter);
        refreshData();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.toolbar_Layout_back) {
            finish();
        }

    }

    List<FollowUserModle.DataBean> data;

    private void refreshData() {

        UserServiceImpl.GetBlackList(page, 20, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                listFenSi.onRefreshComplete();
                try {
                    ResponseBody body1 = response.body();
                    String s = body1.string();
                    FollowUserModle body = new Gson().fromJson(s, FollowUserModle.class);
                    List<FollowUserModle.DataBean> followData = body.getData();
                    if (data == null) {
                        data = new ArrayList<>();
                    }
                    if (page == 1) {
                        data.clear();
                    }
                    if (followData == null||followData.size()==0) {
                        LogUtils.startToash(BlackListActivity.this, "无更多数据");
                    }
                    data.addAll(followData);
                    adapter.setData(data, 1);
                    adapter.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                listFenSi.onRefreshComplete();
            }
        });

    }
}
